package controller

import (
	"gg_server/response"
	"gg_server/util"

	"github.com/gin-gonic/gin"
)

// 上传图片

type upload struct{}

var UploadController *upload

func init() {
	UploadController = &upload{}
}

// 上传图片
func (u *upload) UploadImage(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		// 返回错误
		response.GetError().ParameterError("没有上传图片")
		return
	}

	dst, src := util.Upload.GetUploadDst("image", file, &util.CheckImg)
	// 储存头像
	ctx.SaveUploadedFile(file, dst)

	response.SendSuccessMsg(ctx, &response.SuccessMsg{
		Msg: "上传成功",
		Data: gin.H{
			"image": src,
		},
	})
}
